package net.gamebug.guessthemovie;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import android.os.Handler;

public class Win extends Activity {
	
	//Entra no top Five

    private Boolean started = false;
    
    
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		//System.out.println("########### ON CREATE");
		//started = false;
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
                             WindowManager.LayoutParams.FLAG_FULLSCREEN );
		setContentView(R.layout.win);
        				
		
	
		
                         //   TextView dispscore = (TextView) findViewById(R.id.displayscore);
                         //   dispscore.setText(Long.toString(Global.score));
                            
                             
                            
                            final Button submit = (Button) findViewById(R.id.sendscore);
                            submit.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                           
                            EditText playername = (EditText) findViewById(R.id.playername);
                            Global.pname=playername.getText().toString();
                            insertplayer(Global.pname, Global.score);
                            finish();
                            Global.score=0;
                            startActivity(new Intent("net.gamebug.guessthemovie.MAINMENU"));
            }
        });
	

        }
	
	
	
	
        @Override
	protected void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
		//System.out.println("########### ON START " + started);
		if (!started) showscore();
		started = true;
	}




		public void insertplayer(String name, long score){
            
			bd n = new bd(this);
			n.open();
			long numero= n.insertPlayer(name,score);
			Toast msg = Toast.makeText(this, ""+numero, Toast.LENGTH_LONG);
            msg.setGravity(Gravity.CENTER, msg.getXOffset() / 2, msg.getYOffset() / 2);
            //msg.show();
            android.os.Process.killProcess(android.os.Process.myPid());
        
        
        }
        
		private double scoreI = 0;
		public void showscore() {
		   
		   System.out.println("Pause is:" + Global.pause + "  Stop is:" + Global.stop );
		   Global.dispscore = (TextView) findViewById(R.id.displayscore);
    	   if (Global.pause==false && Global.stop==false){
    		   double inc = (double) Global.score/(500/5);
    		   scoreI+=inc;
    		  //System.out.println(scoreI + " " + inc + " " + ((double) Global.score/(500/5)) + " " + Global.score);
    		   
    		   if (scoreI < Global.score) {
    			   Global.dispscore.setText(Long.toString(Math.round(scoreI)));
    			   Handler handle = new Handler();
    			   handle.postDelayed(new Runnable() {
    				   public void run() {
    					   showscore();
    					   }
    				   }, 5);
    			   } else {
    				   Global.dispscore.setText(Long.toString(Global.score));
    				   }
    		   }else{
    			   Global.dispscore.setText(Long.toString(Global.score));  
    			   }
    	   }
       


	@Override
	public void onBackPressed() {
		// TODO Auto-generated method stub
		super.onBackPressed();
		android.os.Process.killProcess(android.os.Process.myPid());
	}




	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		Global.pause = true;
		}

	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
		Global.stop = true;
		}
    
	
       
}